package cn.iocoder.yudao.module.design.controller.admin.probleminnerrel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.probleminnerrel.vo.*;
import cn.iocoder.yudao.module.design.convert.probleminnerrel.ProblemInnerRelConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.probleminnerrel.ProblemInnerRelDO;
import cn.iocoder.yudao.module.design.service.probleminnerrel.ProblemInnerRelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 问题间关联")
@RestController
@RequestMapping("/design/problem-inner-rel")
@Validated
public class ProblemInnerRelController {

    @Resource
    private ProblemInnerRelService problemInnerRelService;

    @PostMapping("/create")
    @Operation(summary ="创建问题间关联")
    @PreAuthorize("@ss.hasPermission('design:problem-inner-rel:create')")
    public CommonResult<Long> createProblemInnerRel(@Valid @RequestBody ProblemInnerRelCreateReqVO createReqVO) {
        return success(problemInnerRelService.createProblemInnerRel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新问题间关联")
    @PreAuthorize("@ss.hasPermission('design:problem-inner-rel:update')")
    public CommonResult<Boolean> updateProblemInnerRel(@Valid @RequestBody ProblemInnerRelUpdateReqVO updateReqVO) {
        problemInnerRelService.updateProblemInnerRel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除问题间关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:problem-inner-rel:delete')")
    public CommonResult<Boolean> deleteProblemInnerRel(@RequestParam("id") Long id) {
        problemInnerRelService.deleteProblemInnerRel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得问题间关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:problem-inner-rel:query')")
    public CommonResult<ProblemInnerRelRespVO> getProblemInnerRel(@RequestParam("id") Long id) {
        ProblemInnerRelDO problemInnerRel = problemInnerRelService.getProblemInnerRel(id);
        return success(ProblemInnerRelConvert.INSTANCE.convert(problemInnerRel));
    }

    @GetMapping("/list")
    @Operation(summary ="获得问题间关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:problem-inner-rel:query')")
    public CommonResult<List<ProblemInnerRelRespVO>> getProblemInnerRelList(@RequestParam("ids") Collection<Long> ids) {
        List<ProblemInnerRelDO> list = problemInnerRelService.getProblemInnerRelList(ids);
        return success(ProblemInnerRelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得问题间关联分页")
    @PreAuthorize("@ss.hasPermission('design:problem-inner-rel:query')")
    public CommonResult<PageResult<ProblemInnerRelRespVO>> getProblemInnerRelPage(@Valid ProblemInnerRelPageReqVO pageVO) {
        PageResult<ProblemInnerRelDO> pageResult = problemInnerRelService.getProblemInnerRelPage(pageVO);
        return success(ProblemInnerRelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出问题间关联 Excel")
    @PreAuthorize("@ss.hasPermission('design:problem-inner-rel:export')")
   
    public void exportProblemInnerRelExcel(@Valid ProblemInnerRelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProblemInnerRelDO> list = problemInnerRelService.getProblemInnerRelList(exportReqVO);
        // 导出 Excel
        List<ProblemInnerRelExcelVO> datas = ProblemInnerRelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问题间关联.xls", "数据", ProblemInnerRelExcelVO.class, datas);
    }

}
